
export default class FsUtils {

    private static instance: FsUtils;
    public static getInstance() {
        if (!this.instance) {
            this.instance = new FsUtils();
        }
        return this.instance;
    }

    private fs: ApicloudModules['fs'];
    private trans: ApicloudModules['trans'];

    private constructor() {
        this.fs = api.require('fs');
        this.trans = api.require('trans');
        if (!this.fs) {
            throw new Error('fs 模块未引入');
        }
        if (!this.trans) {
            throw new Error('trans 模块未引入');
        }
    }
    /**
     * 判断文件或者文件夹是否存在
     * 
     */
    public exist(params: { path: string }) {
        return new Promise<boolean>((resolve) => {
            this.fs.exist(params, (ret) => {
                resolve(ret.exist)
            })
        });
    }
    /**
     * 重命名
     */
    public rename(params: {
        oldPath: string,
        newPath: string,
    }) {
        return new Promise<{ status: boolean, msg: string }>((resolve) => {
            this.fs.rename(params, (ret, err) => {
                if (ret.status) {
                    resolve({
                        status: true,
                        msg: '成功'
                    });
                } else {
                    resolve({
                        status: false,
                        msg: err.msg
                    });
                }
            });
        });
    }

    /**
     * 拷贝文件
     */
    public copyTo(params: {
        oldPath: string,
        newPath: string,
    }) {
        return new Promise<{ status: boolean, msg: string }>((resolve) => {
            this.fs.copyTo(params, (ret, err) => {
                if (ret.status) {
                    resolve({
                        status: true,
                        msg: '成功'
                    });
                } else {
                    resolve({
                        status: false,
                        msg: err.msg
                    });
                }
            });
        });
    }

    /**
     * base64转本地图片
     * @param base64Str     base64内容
     * @param imgPath       转换后保存的图片路径
     * @param imgName       转换后保存的图片名称
     * 
     * @returns \{status: 是否转换成功, path: 本地图片路径}
     */
    public encodeBase64ToImage(params: {
        base64Str: string;
        imgPath: string;
        imgName: string
    }) {
        return new Promise<{ status: boolean, path: string }>((resolve) => {
            this.trans.saveImage(params, (ret) => {
                if (ret.status) {
                    const isSp = params.imgPath[params.imgPath.length - 1] === '/';
                    const path = params.imgPath + (isSp ? '/' : '') + params.imgName;
                    resolve({
                        status: true,
                        path
                    });
                } else {
                    resolve({
                        status: false,
                        path: ''
                    });
                }
            });
        });
    }

    /**
     * 本地图片转成base64
     * @param imgPath 本地图片路径
     * 
     * @returns \{status: 是否转换成功, base64Str: base64字符串}
     */
    public decodeImageToBase64(params: {
        imgPath: string
    }) {
        return new Promise<{ status: boolean, base64Str: string }>((resolve) => {
            this.trans.decodeImgToBase64(params, resolve);
        });
    }

    /**
     * 网络图片转成base64
     * @param imgPath 本地图片路径
     * 
     * @returns \{status: 是否转换成功, base64Str: base64字符串}
     */
    public decodeNetworkImageToBase64(params: {
        imgPath: string
    }) {
        return new Promise<{ status: boolean, base64Str: string }>((resolve) => {
            if (params.imgPath.substr(0, 4) === 'http') {
                api.imageCache({
                    url: params.imgPath,
                    policy: 'cache_only'
                }, (ret) => {
                    params.imgPath = ret.url;
                    this.trans.decodeImgToBase64(params, resolve);
                });
                return;
            }
            this.trans.decodeImgToBase64(params, resolve);
        });
    }




}
